package top.zlhy7.autoconfig.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import top.zlhy7.autoconfig.prop.MyDataSourceProperties;

import javax.sql.DataSource;

/**
 * @author renyong
 * @date 2022/11/19 11:43
 * @description 自定义数据源自动配置类
 */
//@ConditionalOnProperty(prefix = "spring.myjdbc.data-source",name = "enable",havingValue = "true")
@EnableConfigurationProperties(MyDataSourceProperties.class)
@Configuration
public class MyDataSourceAutoConfig {
    /**
     * 配置类
     */
    @Autowired
    private MyDataSourceProperties myDataSourceProperties;

    @Bean
    public DataSource druidDataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUsername(myDataSourceProperties.getUserName());
        dataSource.setPassword(myDataSourceProperties.getPassword());
        dataSource.setUrl(myDataSourceProperties.getUrl());
        dataSource.setDriverClassName(myDataSourceProperties.getDriverClassName());
        return dataSource;
    }
}
